package com.shinowit.web;

import java.sql.Types;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shinowit.model.Result;

@Controller
@RequestMapping("/default")
public class DefaultPageController {
	
	@Resource
	private JdbcTemplate jt;
	
	@RequestMapping(value="/",method=RequestMethod.GET)
	public String openDefPage(Model model,HttpServletRequest request){
		
		String current_username=(String)request.getSession().getAttribute("current_user");
		
		if (null==current_username){
			//当前用户未登录
			return "redirect:/login/";
		}
		
		//查询所属部门信息
		String user_department_name=jt.queryForObject("select b.org_name from  sys_user a inner join sys_org_info b on a.org_code=b.org_code and a.user_name=?", new Object[]{current_username},new int[]{Types.VARCHAR},String.class);
		model.addAttribute("user_department", user_department_name);
		
		//查询数据库取当前登录用户可以访问的菜单数据
		List<Map<String,Object>> menu_instock=jt.queryForList("select distinct sys_res_code,sys_res_url,sys_res_name from view_user_res where user_name=? and parent_code='02'  order by res_order_id",new Object[]{current_username},new int[]{Types.VARCHAR});
		model.addAttribute("menu_instock", menu_instock);

		List<Map<String,Object>> menu_outstock=jt.queryForList("select distinct sys_res_code,sys_res_url,sys_res_name from view_user_res where user_name=? and parent_code='03' order by res_order_id",new Object[]{current_username},new int[]{Types.VARCHAR});
		model.addAttribute("menu_outstock", menu_outstock);
		
		List<Map<String,Object>> menu_stock=jt.queryForList("select distinct sys_res_code,sys_res_url,sys_res_name from view_user_res where user_name=? and parent_code='04' order by res_order_id",new Object[]{current_username},new int[]{Types.VARCHAR});
		model.addAttribute("menu_stock", menu_stock);

		List<Map<String,Object>> menu_order=jt.queryForList("select distinct sys_res_code,sys_res_url,sys_res_name from view_user_res where user_name=? and parent_code='05' order by res_order_id",new Object[]{current_username},new int[]{Types.VARCHAR});
		model.addAttribute("menu_order", menu_order);

		List<Map<String,Object>> menu_system=jt.queryForList("select distinct sys_res_code,sys_res_url,sys_res_name from view_user_res where user_name=? and parent_code='01' order by res_order_id",new Object[]{current_username},new int[]{Types.VARCHAR});
		model.addAttribute("menu_system", menu_system);
		
		return "/default/default";
	}
	
	@RequestMapping(value="/changepass")
	@ResponseBody
	public Result changePassword(String old_password,String new_password,HttpServletRequest request){
		Result result=new Result("未知错误",false);
		String current_username=(String)request.getSession().getAttribute("current_user");
		try{
			int i=jt.update("update sys_user set login_pass=? where user_name=? and login_pass=?",new Object[]{new_password,current_username,old_password},new int[]{Types.VARCHAR,Types.VARCHAR,Types.VARCHAR});
			if (i>0){
				result.setMsg("修改成功!");
				result.setStatus(true);
			}else{
				result.setMsg("修改密码失败!");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
}
